﻿using Management.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Management.BLL;
using Util.Webs.EasyUi.Trees;
using Util.Webs.EasyUi.Results;
using Util.Webs.EasyUi;
using Management.ViewModel;

namespace MET.Web.Controllers
{
    public class DepartmentsController : Controller
    {
        //
        // GET: /Departments/

        public ActionResult Index()
        {
            return View();
        }
        //Departments
        public ActionResult GetAll()
        {
            DepartmentService service = new DepartmentService();
            var v = service.GetAllDepartments().ToList ();
            return Json(TreeGrid_Department.Totreetgrid(v));
        }

        [HttpPost]
        [UIExceptionResult]
        public ActionResult Add(Department entity)
        {
            if (entity != null && entity.ParentID > 0)
            {
                DepartmentService service = new DepartmentService();
                var result = service.Insert(entity);
                if (result)
                    return Content("OK");
                else
                    return Content("ERROR");
            }
            else
                return Content("ERROR");
        }
        [UIExceptionResult]
        public ActionResult GetInfo(int id)
        {
            DepartmentService service = new DepartmentService();
            var V = service.GetDepartmentsInfo(id);
            return Json(V, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult Update(Department entity)
        {
            DepartmentService service = new DepartmentService();
            var v = service.Update(entity);
            if (v)
            {
                return Content("OK");
            }
            return Content("ERROR");
        }
        [HttpPost]
        public ActionResult Delete(int id)
        {
            DepartmentService service = new DepartmentService();
            var entity = service.GetDepartmentsInfo(id);
            var v = service.Delete(entity.DID);
            if (v)
            {
                return Content("OK");
            }
            return Content("ERROR");
        }
    }
}
